<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      1. sum([1, 2, [10, 20, [3, 4], 5, 6], 7, 8],n=0)
      n = 3, n = 3 + sum([10, 20, [3, 4], 5, 6],3),7,8
      2. sum([10, 20, [3, 4], 5, 6],n=3)
      n = 33, n = 33 + sum([3, 4], 33),  5, 6
      3. sum([3, 4], 33)
      40
       */
      const nums = [1, 2, [10, 20, [3, 4], 5, 6], 7, 8];
      function sum(arr, n = 0) {
        arr.forEach((el) => {
          if (Array.isArray(el) === false) n = n + el;
          else n = n + sum(el, n);
        });
        return n;
      }
      console.log(sum(nums));
    </script>
  </body>
</html>
